#!/bin/bash

# *** This is modified version of the script mentioned below ***

# build.sh -- builds XPI file for mozilla extensions
#   by Nickolay Ponomarev <asqueella@gmail.com>
#   (original version based on Nathan Yergler's build script)
# Most recent version is at <http://kb.mozillazine.org/Bash_build_script>

# This script assumes the following directory structure:
# ./
#   chrome.manifest
#   install.rdf
#   (other files listed in $ROOT_FILES)
#
#   content/    chrome content, defined in $CHROME_CONTENT
#   locale/     chrome locales, defined in $CHROME_LOCALE
#   skin/       chrome skin, defined in $CHROME_SKIN
#
#   defaults/   |
#   components/ |} other directories must be listed in $ROOT_DIRS in order to be packaged
#   ...         |
#
# It uses a temporary directory ./build when building; don't use that!
# Script's output is:
# ./$APP_NAME.xpi
# ./files -- the list of packaged files
#

#
# default configuration file is ./config_build.sh, unless another file is 
# specified in command-line. Available config variables:
APP_NAME=          # short-name and xpi file name. Must be lowercase with no spaces
CHROME_CONTENT=    # chrome content directory
CHROME_LOCALE=     # chrome locale directory
CHROME_SKIN=       # chrome skin directory
CLEAN_UP=          # delete the "files" file when done?       (1/0)
ROOT_FILES=        # put these files in root of xpi (space separated list of leaf filenames)
ROOT_DIRS=         # ...and these directories       (space separated list)
BEFORE_BUILD=      # run this before building       (bash command)
AFTER_BUILD=       # ...and this after the build    (bash command)

if [ -z $1 ]; then
  . ./config_build.sh
else
  . $1
fi

if [ -z $APP_NAME ]; then
  echo "You need to create build config file first!"
  echo "Read comments at the beginning of this script for more info."
  exit;
fi

ROOT_DIR=`pwd`
TMP_DIR=build

#uncomment to debug
#set -x

# remove any left-over files from previous build
rm -f $APP_NAME.xpi files
rm -rf $TMP_DIR

$BEFORE_BUILD

mkdir --parents --verbose $TMP_DIR

# collect chrome content and skin files, exlude CVS, SVN, and temporary files
for CHROME_SUBDIR in "$CHROME_CONTENT $CHROME_SKIN"; do
  find $CHROME_SUBDIR \( -path '*CVS*' -o -path '*.svn*' \) -prune -o -type f -print | grep -v \~ >> files
done

# collect chrome locale files defined in chrome.manifest
LOCALES="$(awk '/^locale/ { print $3 }' < chrome.manifest)"
for LOCALE in $LOCALES; do
  find $CHROME_LOCALE/$LOCALE -path '*.dtd' -o -path '*.properties' >> files
done

cp --verbose --parents `cat files` $TMP_DIR

# prepare components and defaults
echo "Copying various files to $TMP_DIR folder..."
for DIR in $ROOT_DIRS; do
  mkdir $TMP_DIR/$DIR
  FILES="`find $DIR \( -path '*CVS*' -o -path '*.svn*' \) -prune -o -type f -print | grep -v \~`"
  echo $FILES >> files
  cp --verbose --parents $FILES $TMP_DIR
done

# Copy other files to the root of future XPI.
for ROOT_FILE in $ROOT_FILES install.rdf chrome.manifest; do
  cp --verbose $ROOT_FILE $TMP_DIR
  if [ -f $ROOT_FILE ]; then
    echo $ROOT_FILE >> files
  fi
done

cd $TMP_DIR

# generate the XPI file
echo "Generating $APP_NAME.xpi..."
zip -r -D ../$APP_NAME.xpi *

cd "$ROOT_DIR"

echo "Cleanup..."
if [ $CLEAN_UP = 1 ]; then
  rm ./files
fi

# remove the working files
rm -rf $TMP_DIR
echo "Done!"

$AFTER_BUILD
